﻿// Apache-2.0 License
// Copyright (c) 2021-2022 朱飞龙
// 电话/微信：18706708563  QQ群：136192142

using Furion.Schedule;
using Furion.TaskQueue;
using Radius.NET.Interface.Kingdee.BasicInfoModule;
using Radius.NET.Interface.Kingdee.Service.BasicInfoModule;
using Radius.NET.Interface.Kingdee.Service.Qc;

namespace Radius.NET.Interface.Job;

[JobDetail("sync_kingdee_inventorylist", Description = "同步金蝶物料档案", GroupName = "default", Concurrent = false)]
[Daily(TriggerId = "trigger_sync_kingdee_inventorylist", Description = "同步金蝶物料档案")]
//[PeriodMinutes(1, TriggerId = "trigger_sync_kingdee_inventorylist", Description = "同步金蝶物料档案")]

public class SyncKindeeInventoryListJob : IJob
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ITaskQueue _taskQueue;

    public SyncKindeeInventoryListJob(IServiceProvider serviceProvider, ITaskQueue taskQueue)
    {
        _serviceProvider = serviceProvider;
        _taskQueue = taskQueue;
    }
    public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
    {
        //同步一周之内的数据
        string date = context.OccurrenceTime.AddDays(-7).ToString("yyyy-MM-dd");

        #region 同步物料档案
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var inventoryService = serviceScope.ServiceProvider.GetService<InventoryService>();

            //同步物料档案
            $"【{DateTime.Now}】同步物料档案".LogInformation<InventoryService>();
            await inventoryService.SyncInventoryList(39, date);
        });
        #endregion

        #region 同步供应商档案
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var bascInfoService = serviceScope.ServiceProvider.GetService<MisVendorService>();

            $"【{DateTime.Now}】同步供应商档案".LogInformation<MisVendorService>();

            //同步供应商档案
            await bascInfoService.SyncMisVendorList(39, date);
        });
        #endregion

        #region 同步客户档案
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var bascInfoService = serviceScope.ServiceProvider.GetService<MisCustomerService>();
            $"【{DateTime.Now}】同步客户档案".LogInformation<MisCustomerService>();

            //同步客户档案
            await bascInfoService.SyncMisCustomerList(39, date);
        });
        #endregion

        #region 同步配方档案
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var bascInfoService = serviceScope.ServiceProvider.GetService<ProductBomService>();

            $"【{DateTime.Now}】同步配方档案".LogInformation<ProductBomService>();

            //同步配方档案
            await bascInfoService.SyncBomList(39, date);
        });
        #endregion

        #region 同步库位档案
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var bascInfoService = serviceScope.ServiceProvider.GetService<StockService>();

            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("【" + DateTime.Now + "】同步库位档案");

            $"【{DateTime.Now}】同步库位档案".LogInformation<StockService>();

            //同步库位档案
            await bascInfoService.SyncStockList(39);
        });
        #endregion

        #region 同步金蝶质检
        await _taskQueue.EnqueueAsync(async (provider, token) =>
        {
            using var serviceScope = _serviceProvider.CreateScope();
            var bascInfoService = serviceScope.ServiceProvider.GetService<QmInspectBillService>();

            $"【{DateTime.Now}】同步金蝶质检单".LogInformation<QmInspectBillService>();

            //同步两天之内的数据
            date = context.OccurrenceTime.AddDays(-1).ToString("yyyy-MM-dd");

            //同步金蝶质检单
            await bascInfoService.GetQmInspectList(39, date, string.Empty);
            await _taskQueue.DequeueAsync(token);
        });
        #endregion

    }
}
